package com.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.manage.mapper.CouponMapper;
import com.manage.pojo.entity.TbCoupon;
import com.manage.pojo.entity.TbMemberLevel;
import com.manage.pojo.vo.CouponVo;
import com.manage.pojo.vo.UniqueVo;
import com.manage.service.CouponService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: clms-third
 * @description: 优惠券业务层
 * @author: 林子洁
 * @create: 2021-07-23 20:37
 **/
@Service
public class CouponServiceImpl implements CouponService {
    @Resource
    private CouponMapper couponMapper;

    @Override
    public R initCoupon(Integer currentPage, Integer pageSize, CouponVo couponVo) {
        //模糊查询 账户和昵称
        QueryWrapper<TbCoupon> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(couponVo.getName())) {
            wrapper.like("name", couponVo.getName());
        }
        //分页
        Page<TbCoupon> couponPage = new Page<>(currentPage, pageSize);
        couponMapper.selectPage(couponPage, wrapper);
        return R.ok().put("records", couponPage.getRecords()).put("total", couponPage.getTotal());
    }

    @Override
    public R insertCoupon(TbCoupon tbCoupon) {
        couponMapper.insert(tbCoupon);
        return R.ok().message("好了");
    }

    @Override
    public R updateCoupon(TbCoupon tbCoupon) {
        couponMapper.updateById(tbCoupon);
        return R.ok().message("好了");
    }

    @Override
    public R deleteCoupon(Integer id) {
        couponMapper.deleteById(id);
        return R.ok().message("好了");
    }

    @Override
    public R selectByCouponId(Integer id) {
        TbCoupon tbCoupon = couponMapper.selectById(id);
        return R.ok().put("couponInfo", tbCoupon);
    }

    @Override
    public R checkName(UniqueVo uniqueVo) {
        QueryWrapper queryName=new QueryWrapper();
        queryName.eq("name",uniqueVo.getName());
        TbCoupon tbCoupon = couponMapper.selectOne(queryName);
        if(tbCoupon==null){
            return R.ok().put("type", true);
        }else {
            if(String.valueOf(tbCoupon.getId()).equals(uniqueVo.getId())){
                return R.ok().put("type", true);
            }else {
                return R.error().put("type", false);
            }
        }
    }
}
